/**
* @Title: warning_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('ConclusionApprovementCtrl', ['$scope','$http','$modal','BaseURL','Modal','CaseCommonService','WarningAddService','FwaTabsetService',
	                                             'AuditDictServices','$stateParams','$state','WarningScreenService',
	                                function($scope,$http,$modal,baseUrl,Modal,CaseCommonService,WarningAddService,fwaTabsetService,
	                                		AuditDictServices,$stateParams,$state,WarningScreenService){
		
		

		$scope.problemProcessOpinion={}; // 处理意见
		$scope.problemAuditOpinion={}; // 审核意见
		$scope.surveyResult={}; // 调查详情
		$scope.surveyReview={}; // 调查复核
		$scope.surveyAudit={}; // 调查审核
		$scope.auditProjectList=[]; // 稽核项目列表
		$scope.caseCheckResult={}; // 稽核结果
		$scope.caseReviewResult={}; // 稽核复核结果
		$scope.caseApproveResult={}; // 稽核结论审批意见
		$scope.caseAuditResult={}; // 稽核审核意见
		$scope.auditDictServices = AuditDictServices;
        AuditDictServices.initCodeListDatas();
		
        $scope.auditSummary = null;//不合理费用
        $scope.ammountCount = 0;//不合理费用
		$scope.dispatchButton=true;
		$scope.confirmButton=true;
		
		$scope.auditObj={}; //审核对象
		
		/**
		 * 事件结束  ---> 调用事件处理tab 页面的时候需要暂时
		 */
		$scope.problemStatus = 7;
		
        $scope.problemtab=[ 
                         {label:'事件处理',id:0,status:1,url:'views/hcpms/audit/problemSearch/caseCheckProblemDeal.html'},
     		             {label:'调查结果',id:1,status:3,url:'views/hcpms/audit/problemSearch/caseCheckSurveyResult.html'},
     		             {label:'稽核项目',id:2,status:3,url:'views/hcpms/audit/caseCheck/caseCheckAuditPorject_view.html'},
     		             {label:'稽核结果',id:3,status:3,url:'views/hcpms/audit/caseSearch/caseCheckAuditResult_view.html'},
     		             {label:'审批意见',id:4,show:1,url:'views/hcpms/audit/conclusionApprovement/caseAduitCaseAprmAdd_edit.html'}];

        $scope.tabClicks = function(tab) {
            $scope.selectedTabs = tab;
            $scope.tabIndex = tab.id;
            setTimeout(function(){
				$(window).trigger('resize');
            },0);
        }
        $scope.tabClicks($scope.problemtab[4]);
        
        
        $scope.columns = [
                          {label:'项目名称',map:'projectName',width:'20%'},
                          {label:'结算方式',map:'treatTypeName',width:'20%'},
                          {label:'不合理费用',map:'deductionAmmountStr',width:'20%'},
                          {label:'备注',map:'remark',width:'40%'}
                         ];
       $scope.tableData = [];
       
          //初始化请求参数
	    function params(){
			var caseCode = "";
			if($stateParams.caseCode && !angular.isUndefined($stateParams.caseCode) ){
				caseCode = $stateParams.caseCode;
				$scope.caseCode=caseCode;
				$scope.problemCode=$stateParams.problemCode;
				
			}
			var params = {
					caseCode:caseCode,
					problemCode:$scope.problemCode
			}
			return params;
		}
       

	    /**
	     * 是否展示复核意见  3:复核  4:审核
	     */
	    $scope.chuckOpione = 6;
	    
       
       /**
        * 初始化案件对象
        */
       function initCaseInfo(){
    	   var parameter=params();
    	   CaseCommonService.loadCaseData(parameter).success(function(response){ 
    		   //处理tab页
    		   var _obj = response.result.case1;
    		   $scope.auditSummary = _obj.auditSummary;
    		 //处理小数点
    		   var str = parseFloat(_obj.totalDeduction)+"";
	       		var la = str.split(".");
	       		if(la.length==1){
	       			str+=".00";
	       		}
    		   $scope.totalDeduction = str;
    		   _obj.problemStart = _obj.periodBeginDate;
    		   _obj.problemEnd = _obj.periodEndDate;
    		   _obj.problemNo = _obj.relatedCode;
    		   $scope.object= _obj;
			});
    	   
       }
       
       /**
        * 初始化医疗机构,医务人员,参保人信息
        */
       function initMedicalAndStaffAndInsurance(){
    	   var obj=params();
    	   var parameter={
    			   problemNo:obj.caseCode,
    			   taskType:'2'             //案件
    	   };
			CaseCommonService.loadMedicalInfoList(parameter).success(function(response){   	 
				
				$scope.medicalData2=response.medicalBean.medicalList;
				$scope.staffData2=response.staffBean.staffList;
				$scope.insuranceData2=response.insuranceBean.insuranceList;
				//更新当前页面数据
				WarningScreenService.findPointCutTempScose(
	     				   WarningAddService.warningMarks,$stateParams.caseCode,'02',
	     				   $scope.medicalData2[0],$scope.staffData2,$scope.insuranceData2);
			});
       }
       /**
        * 初始化各个tab 信息
        */
       function initTabInfo(){
    	   var param = {caseCode:$stateParams.caseCode,problemNo:$stateParams.problemCode};
    	   CaseCommonService.queryAllOpinion(param).success(function(response){   	 
    		   resetEachTabData(response);
			});
       }
       
   	/**
      * 0-初始任务分配1-调查初审
		2-事件审核3-调查结论4-调查结论复核
		5-调查结论审核
		10-稽核分配
		11-稽核结论
		12-稽核结论复核
		13-稽核结论审核
		14-医疗机构确认稽核结论
		15-稽核结论审批
   	 */
       function resetEachTabData(_array){
    	   if(_array!= null && _array.length >0){
    		   angular.forEach(_array,function(data,index){
    			   if(data.opinionType == 1){
    				   $scope.problemProcessOpinion = data;
    			   }else if(data.opinionType == 2){
    				   $scope.problemAuditOpinion = data;
    			   }else if(data.opinionType == 3){
    				   $scope.surveyResult = data;
    			   }else if(data.opinionType == 4){
    				   $scope.surveyReview = data;
    			   }else if(data.opinionType == 5){
    				   $scope.surveyAudit = data;
    			   }else if(data.opinionType == 15){
    				   $scope.caseApproveResult = data;
    			   } 
    		   });
    	   }
       }
       
       
       /**
        * 初始化所有的数据
        */
       function initAllData(){
    	  initCaseInfo();
    	  initMedicalAndStaffAndInsurance();
    	  initTabInfo();
       }
       
       initAllData();
       
       
       /**审核意见选择监听函数***/
		$scope.selectChangeFun=function(){
			
			if($scope.auditObj){
				var opinion=$scope.auditObj.opinion;
				if(opinion && opinion=='1'){ //同意
					$scope.confirmButton=true;
					$scope.dispatchButton=false;
				}else{
					$scope.confirmButton=false;
					$scope.dispatchButton=true;
				}
			}
			
		}
       
		
		/**
	        * 
	        * @param response
	        */
	       function caseResponseResult(response){
	    	   if(response.auditProjectList){
	    		   $scope.tableData=response.auditProjectList;
	    	   }
	    	   if(response.caseCheckResult){
	    		   $scope.caseCheckResult=response.caseCheckResult;
	    	   }
	    	   if(response.caseReviewResult){
	    		   $scope.caseReviewResult=response.caseReviewResult;
	    	   }
	    	   
	    	   if(response.caseApproveResult){
	    		   $scope.caseApproveResult=response.caseApproveResult;
	    	   }
	    	   
	    	   if(response.caseAuditResult){
	    		   $scope.caseAuditResult=response.caseAuditResult;
	    	   }
	    	   
	       }
		
       /**
        * 返回事件
        */
       $scope.goBack =function(){
           if($stateParams.urlFlag){
               var urlFlag =  $stateParams.urlFlag;
               fwaTabsetService.closeTab('conclusionApprovement','workingArea.approvalPost',{urlFlag:urlFlag});
           }else
           {
        	   $state.go('conclusionApprovement.conclusionApprovementListCtrl');
           }
       }
       
       
       /**
        * 查看案件信息对象中案件来源对话框
        */
       $scope.$on('fwaAuditLeft.showModal',function(){
    	   $scope.caseObj=$scope.object;
    	   CaseCommonService.showCaseFromSourceDialog($scope);
       });
       
       
       /**
        * 初始化各个tab 信息
        */
       function initCaseTabInfo(){
    	 var parameteObject=params();
    	   $http({
				method : 'post',
				cache : false,
				url : baseUrl + 'caseCheckAudit/loadCaseInfo.do',
				params :parameteObject
			}).success(function(response, status, headers, config) {
				caseResponseResult(response);
			});
       }
        
       /**
        * 展现稽核报告对话框
        */
       $scope.showCaseAuditReportDialog=function(){
       	 CaseCommonService.showCaseAuditReportDialog($scope);
       }
       
       /**
        * 展现稽核告知函
        */
       $scope.showCaseAuditTellReportDialog=function(){
    	   ininAuditNotifiaction();
    	   CaseCommonService.showCaseAuditTellReportDialog($scope);
       }
       
       /**
        * 初始化稽核告知函的报告
        */
       function ininAuditNotifiaction(){
       	var parameter={
       			caseCode:$stateParams.caseCode
       	};
         CaseCommonService.loadAuditNotifionInfo(parameter).success(function(response){   
       	  
       	  
       	  
				$scope.reportBean=response.reportBean;
				$scope.caseDetail=response.tellReportBean;
				
				$scope.reportBean.auditRang={
		       			   start:$scope.reportBean.auditStart,
		       			   end:$scope.reportBean.auditEnd
		       	      };
			});
       }
       
       function check()
       {
           if(!$scope.caseApproveResult.opinion)
           {
               Modal.alert('请填写审批意见!');
               return false;
           } else if(!$scope.caseApproveResult.opinionDesc)
           {
               Modal.alert('请填写意见说明！');
               return false;
           } 
           return true;
       }

       /**
        * 暂存
        */
       $scope.onlySave = function(){
       	if(!check()){
       		return;
       	}
       	var opinionDTO = new Object();
       	opinionDTO.problemNo=$stateParams.caseCode;
       	opinionDTO.caseCode=$stateParams.caseCode;
       	opinionDTO.opinion = $scope.caseApproveResult.opinion;
       	opinionDTO.opinionDesc = $scope.caseApproveResult.opinionDesc;
       	opinionDTO.onlySave = 1;
       	//从初审变更到待审核
       	opinionDTO.statusAfter = 7;
       	//意见信息调查初审
       	opinionDTO.opinionTypeCurrent = 15;
        $scope.loading = true;
       	CaseCommonService.approvementAuditOpinion(opinionDTO).success(function(data){
               $scope.loading = false;
               Modal.alert(data.msg);
               $scope.tabClicks($scope.problemtab[4]);
           });
       }
       
       /**
        * 提交
        */
       $scope.submitApr = function(){
       	if(!check()){
       		return;
       	}
       	var opinionDTO = new Object();
       	opinionDTO.problemNo=$stateParams.caseCode;
       	opinionDTO.caseCode=$stateParams.caseCode;
       	opinionDTO.opinion = $scope.caseApproveResult.opinion;
       	opinionDTO.opinionDesc = $scope.caseApproveResult.opinionDesc;
       	opinionDTO.onlySave = 0;
       	//从初审变更到待审核
       	opinionDTO.statusAfter = 7;
       	//意见信息调查初审
       	opinionDTO.opinionTypeCurrent = 15;
        $scope.loading = true;
       	CaseCommonService.approvementAuditOpinion(opinionDTO).success(function(data){
               $scope.loading = false;
               Modal.alert(data.msg);
               $scope.goBack();
           });
       	
       }
       
       /**
        * 点击医疗机构量事件
        * 
        */
       $scope.$on('fwaInvolve.medicalCountClick',function(){
    	   CaseCommonService.viewMedicalDetail($scope);
       });
		
        /**
         * 点击医务人员总数量事件
         */
        $scope.$on('fwaInvolve.staffCountClick',function(){
        	CaseCommonService.viewStaffList($scope);
        });
        
        /**
         * 点击参保人总数量事件
         */
        $scope.$on('fwaInvolve.insuranceCountClick',function(){
        	CaseCommonService.viewInsurList($scope);
        });
       
       /**
        * 初始化所有的数据
        */
       function initAllData(){
    	  initCaseInfo();
    	  initMedicalAndStaffAndInsurance();
    	  initTabInfo();
    	  initCaseTabInfo();
       }
       
       initAllData();
     
       
       
       // 	end line	
	}]);
})();
